package cn.dlc.com.${packageName}.controller;

import cn.dlc.com.base.controller.BaseController;
import cn.dlc.com.config.response.ResponseHttpStatus;
import cn.dlc.com.config.response.ResponseVO;
import cn.dlc.com.${packageName}.dto.${entityName}DTO;
import cn.dlc.com.${packageName}.entity.${entityName}Entity;
import cn.dlc.com.${packageName}.service.I${entityName}Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: ${authorName}
 * @Date: ${datetime}
 * @DESC: ${description}
 */
@RequestMapping("sys/user")
@RestController
@Api(tags="${description}")
@Slf4j
public class ${entityName}Controller extends BaseController {

    @Autowired
    private I${entityName}Service userService;


    /**
     * 分页
     * @param ${lowerEntityName}DTO
     * @return 分页数据
     */
    @GetMapping("page")
    @ApiOperation("获取分页数据")
    public ResponseVO<IPage<${entityName}Entity>> page(${entityName}DTO ${lowerEntityName}DTO){



        return  new ResponseVO().success(null);
    }

    /**
     * 列表
     * @param userDTO
     * @return 列表数据
     */
    @GetMapping("list")
    @ApiOperation("获取列表数据")
    public ResponseVO<List<UserEntity>> list(UserDTO userDTO){

        List<UserEntity> list = userService.list();

        return ResponseVO.success(list);
    }

    /**
     * 获取指定记录
     * @param id 主键
     * @return 当前对象
     */
    @GetMapping("{id}")
    @ApiOperation("根据id获取当前对象")
    public ResponseVO<UserEntity> findById(@ApiParam("主键") @PathVariable("id") String id){

        if(StringUtils.isNotBlank(id)) {
            UserEntity userEntity = userService.getById(id);
            return ResponseVO.success(userEntity);
        }

        return ResponseVO.error(ResponseHttpStatus.ARGS_ERROR);
    }

    /**
     * 保存
     * @param userDTO
     * @return 当前对象
     */
    @PostMapping
    @ApiOperation("保存当前对象")
    public ResponseVO<UserEntity> save(@RequestBody UserDTO userDTO){

        if(userDTO != null) {
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(userDTO, userEntity);
            boolean saveResult = userService.save(userEntity);
            if(saveResult) {
                return ResponseVO.success(ResponseHttpStatus.SAVE_SUCCESS,userEntity);
            }
        }
        return ResponseVO.error(ResponseHttpStatus.SAVE_ERROR);
    }

    /**
     * 更新
     * @param userDTO
     * @return 当前对象
     */
    @PutMapping
    @ApiOperation("更新当前对象")
    public ResponseVO<UserEntity> update(@RequestBody UserDTO userDTO){

        if(userDTO != null) {
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(userDTO, userEntity);
            boolean saveResult = userService.updateById(userEntity);
            if(saveResult) {
                return ResponseVO.success(ResponseHttpStatus.SAVE_SUCCESS,userEntity);
            }
        }
        return ResponseVO.error(ResponseHttpStatus.SAVE_ERROR);
    }

    /**
     * 删除指定记录
     * @param id 主键
     * @return 当前对象
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除当前对象")
    public ResponseVO<UserEntity> deleteById(@ApiParam("主键")@PathVariable("id") String id){

        if(StringUtils.isNotBlank(id)) {
            boolean removeResult = userService.removeById(id);
            if(removeResult) {
                return ResponseVO.success(ResponseHttpStatus.DELETE_SUCCESS);
            }
        }

        return ResponseVO.success(ResponseHttpStatus.DELETE_ERROR);
    }

    /**
     * 导出excel
     * @param userDTO
     * @return
     */
    @PostMapping("excel")
    @ApiOperation("导出excel")
    public void exportExcel(@RequestBody UserDTO userDTO){

        // 获取生成的文件，然后直接导出。



    }

    /**
     * 获取pdf，这个是不是放这里还待定，因为pdf可能放在文件服务器管理。
     * 如果要做权限控制的话，那么这个可能就需要通过这种方式实现。
     * @param id
     * @return pdf路径
     */
    @GetMapping("pdf/{id}")
    @ApiOperation("获取对应的pdf文件")
    public ResponseVO<String> exportPdf(@ApiParam("主键")@PathVariable("id") String id){

        if(StringUtils.isNotBlank(id)) {
            return ResponseVO.success("http://www.baidu.com");
        }
        return ResponseVO.error(ResponseHttpStatus.ARGS_ERROR);
    }




}
